package com.antis.olsl.magic.analysis;

import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.antis.olsl.arouterlib.MagicRoutePath;
import com.antis.olsl.base.BaseActivity;
import com.antis.olsl.base.utils.ToastUtil;
import com.antis.olsl.magic.R;
import com.antis.olsl.magic.adapter.Top60DetailAdapter;
import com.antis.olsl.magic.databinding.ActivityViolenceAnalysisDetailListBinding;
import com.antis.olsl.magic.response.GetTopAnalysisDetailResp;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.trello.rxlifecycle2.LifecycleTransformer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import timber.log.Timber;

@Route(path = MagicRoutePath.ACTIVITY_VIOLENCE_ANALYSIS_DETAIL_LIST)
public class ViolenceAnalysisDetailListActivity extends BaseActivity<ActivityViolenceAnalysisDetailListBinding> implements ViolenceAnalysisDetailListContract.View {

    @Autowired
    String startDate;
    @Autowired
    String endDate;
    @Autowired
    String showStartDate;
    @Autowired
    String showEndDate;
    @Autowired
    String name;
    @Autowired
    String id;

    private ViolenceAnalysisDetailListPresenter mPresenter;

    private int pageNum = 1;
    private boolean isEnd;
    private Top60DetailAdapter mAdapter;
    private List<GetTopAnalysisDetailResp.ContentBean> mList = new ArrayList<>();

    @Override
    protected ActivityViolenceAnalysisDetailListBinding getViewBinding() {
        return ActivityViolenceAnalysisDetailListBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    protected void initPresenter() {
        super.initPresenter();
        mPresenter = new ViolenceAnalysisDetailListPresenter();
        mPresenter.takeView(this);
    }

    @Override
    protected void initViews() {
        mAdapter = new Top60DetailAdapter(R.layout.item_table, mList);
        viewBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        viewBinding.recyclerView.setAdapter(mAdapter);
//        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
//            @Override
//            public void onLoadMoreRequested() {
//                mRecyclerView.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (isEnd) {
//                            mAdapter.loadMoreEnd();
//                        } else {
//                            loadItemList();
//                        }
//                    }
//                }, 200);
//            }
//        }, mRecyclerView);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GetTopAnalysisDetailResp.ContentBean bean = mList.get(position);
                ARouter.getInstance()
                        .build(MagicRoutePath.ACTIVITY_COMMODITY_DETAIL)
                        .withSerializable("bean", bean)
                        .navigation();
            }
        });
    }

    @Override
    protected void initData() throws IllegalAccessException {
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(showStartDate) || TextUtils.isEmpty(showEndDate)) {
            finish();
        }

        String date = showStartDate + "至" + showEndDate;
        viewBinding.textDate.setText(date);
        viewBinding.textCommodityName.setText(name);

        loadItemList();
    }

    private void loadItemList() {
        Map<String, Object> map = new HashMap<>(6);
        map.put("shopId", id);
        map.put("startTime", startDate);
        map.put("endTime", endDate);
        map.put("analysisType", "4");
        map.put("pageNum", pageNum);
        map.put("pageSize", 10);
        mPresenter.getItemList(map);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mPresenter) {
            mPresenter.unSubscribe();
        }
    }

    @Override
    public void getItemListSuccess(List<GetTopAnalysisDetailResp.ContentBean> list) {
        if (pageNum == 1) {
            mList.clear();
        }
        if (list.size() > 0) {
            mList.addAll(list);
        }
//        if (list.size() < 10) {
//            isEnd = true;
//        } else {
//            isEnd = false;
//            pageNum += 1;
//        }
        mAdapter.setList(mList);
        mAdapter.getLoadMoreModule().loadMoreComplete();
    }

    @Override
    public void getItemListFailure(String msg) {
        Timber.tag("hhh").e("getItemListFailure msg= " + msg);
        if (TextUtils.isEmpty(msg)) {
            msg = "请求失败,请稍后重试";
        }
        ToastUtil.showToast(this, msg);
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }
}